define(function (require, exports, module) {
    var angular = require("angular");
    var ui = require('ui');
    var util = require('util');
    require("angular-ui");
    var dialog = require("dialog");
    var api_prefix = G_CONFIG["api_prefix"] + "/qmyx/para-setting/"; //api前缀

    angular.module('PublicBanksBorrow', [])
        .controller('PublicBanksBorrowController', ['$scope', '$http', function ($scope, $http) {
            $scope.setEnable = function (item) {
                postData(
                    'enable-cim-data',
                    {
                        id: item.myParamValueId,
                        type: 'banks'
                    },
                    '设置成功',
                    true
                )
            };
            $scope.setDefault = function (item) {
                postData(
                    'set-cim-default',
                    {
                        id: item.myParamValueId,
                        type: 'banks'

                    },
                    '设置成功',
                    true
                );
            };


            //佣金发放模式选择
            var paymentType = function (newValue) {
                var data = $scope.data;

                var title = "佣金发放模式";
                var str = '<div>您切换佣金发放模式，会影响发放流程和操作方式。确认切换吗？';
                var d = dialog({
                    title: title,
                    content: str,
                    fixed: true,
                    width: 400,
                    height: 60,
                    okValue: "保存",
                    ok: function () {
                        var $content = this._$('content');
                        $http.post(api_prefix + "save-yjff",
                            {
                                id: data.yjff.id,
                                value: data.yjff.value
                            }
                        )
                            .success(function (res) {
                                if (res.retCode == 0) {
                                    ui.showSucTip();
                                } else {
                                    ui.showErrTip(res.errMsg);
                                }
                            });
                    },
                    cancel: function () {
                        $scope.data.yjff.value = newValue == '0' ? '1' : '0';
                        $scope.$applyAsync();
                    }
                });
                d.showModal();
            };
            //未开通代支付功能提示
            function notOpenIep() {
                var title = "提示";
                var str = '<div><p>您尚未开通代支付功能，无法切换佣金发放模式，如需开通代</p>'
                    + '<p>支付服务，请咨询明源云客实施顾问或联系400客服团队。</p></div>';
                var _this = this;
                var d = dialog({
                    title: title,
                    content: str,
                    fixed: true,
                    width: 400,
                    height: 60,
                    cancelValue: "关闭",
                    cancel: function () {

                    }
                });
                d.showModal();

            };

            $scope.yjffChange = function (value) {
                if (value === '1' && $scope.data.yjff.isOpen == 0) {
                    notOpenIep();

                    $scope.data.yjff.value = '0';
                } else {
                    paymentType(value);
                }
            };

            $scope.edit = function (item) {
                postData(
                    'edit-cim-data',
                    {
                        id: item.myParamValueId,
                        text: item.Text,
                        value: item.Value,
                        order: item.Order,
                        desc: item.Description
                    },
                    '新增成功',
                    true);
            };

            $scope.add = function (item) {
                postData(
                    'add-cim-data',
                    {
                        id: item.myParamValueId,
                        text: item.Text,
                        value: item.Value,
                        order: item.Order,
                        desc: item.Description,
                        type: 'banks'
                    },
                    '新增成功',
                    true);
            };

            $scope.setDisable = function (item) {
                util.confirm('确定禁用吗？', {
                    ok: function () {
                        postData('disable-cim-data', {
                            id: item.myParamValueId

                        }, '设置成功', true);
                    }
                })
            };

            $scope.delete = function (item) {
                util.confirm('确定删除吗？', {
                    ok: function () {
                        postData('del-cim-data', {
                            id: item.myParamValueId

                        }, '删除成功', true);
                    }
                })
            };

            $scope.moveDown = function (item) {
                var list = $scope.list;
                var curPos = util.indexOf(item, list);
                var curOrder = item.Order;
                var NextOrder = list[curPos + 1].Order;
                if (curOrder == NextOrder) {
                    if (curPos < list.length - 1) {
                        list[curPos] = list[curPos + 1];
                        list[curPos + 1] = item;
                        ui.showSucTip('下移成功');
                    }
                } else {
                    postData(
                        'down-move',
                        {
                            id: item.myParamValueId,
                            type: 'banks',
                            proId: $scope.curProjectId
                        },
                        '下移成功',
                        false,
                        function (res) {
                            if (typeof res.data === 'undefined') {
                                getData();
                            }
                        });
                }
                ;
            };

            $scope.moveUp = function (item) {
                var list = $scope.list;
                var curPos = util.indexOf(item, list);
                var curOrder = item.Order;
                var preOrder = list[curPos - 1].Order;
                if (curOrder == preOrder) {
                    if (curPos > 0) {
                        list[curPos] = list[curPos - 1];
                        list[curPos - 1] = item;
                        ui.showSucTip('上移成功');
                    }

                } else {
                    postData(
                        'up-move',
                        {
                            id: item.myParamValueId,
                            type: 'banks',
                            proId: $scope.curProjectId
                        },
                        '上移成功',
                        false,
                        function (res) {
                            if (typeof res.data === 'undefined') {
                                getData();
                            }
                        });
                }
            };
            $scope.reset = function () {
                util.confirm('确定还原备选项吗？', {
                    ok: function () {
                        postData('reduction-option', {
                            type: 'banks'
                        }, '还原备选项成功', true);
                    }
                })
            };

            /**
             * @param {string} urlPath
             * @param {Object} data
             * @param {string} sucTip
             * @param {boolean=} updateData
             * @param {function=} sucCallback
             */
            function postData(urlPath, data, sucTip, updateData, sucCallback) {
                $http
                    .post(G_CONFIG.api_prefix + "qmyx/para-setting/" + urlPath, data)
                    .success(function (res) {
                        var retCode = res.retCode;
                        if (retCode == 0) {
                            if (updateData) {
                                getData();
                            }

                            sucCallback && sucCallback(res);

                            ui.showSucTip(sucTip);
                        } else {
                            ui.showErrTip(res.errMsg);
                        }
                    })
            }

            $scope.loading = false;
            /**
             * 获取数据
             * @param {string=} projectId
             */
            var getData = function () {
                $scope.loading = true;
                $http
                    .get(api_prefix + "get-jieyong-manage")
                    .then(function (res) {
                        var retCode = res.data.retCode;
                        if (retCode == 0) {
                            var data = res.data.data;
                            $scope.data = data;
                            var list = data.bank;
                            for (var i = 0, item; item = list[i]; i++) {
                                item.Order = parseInt(item.Order);
                            }
                            ;
                            list.sort(function (a, b) {
                                return a.Order - b.Order
                            });
                            $scope.list = list;

                        } else {
                            ui.showErrTip(res.data.errMsg);
                            return false;
                        }
                    })
                    .finally(function () {
                        $scope.loading = false;
                    })
            };
            getData();
        }])
});